﻿using System;
using System.Web;
using System.Configuration;

namespace OF.Utility
{
    public class SiteUtility
    {

        #region 常用属性
        public static HttpContext CurContext
        {
            get { return HttpContext.Current; }
        }

        public static HttpRequest CurHttpRequest
        {
            get { return HttpContext.Current.Request; }
        }

        public static HttpResponse CurHttpResponse
        {
            get { return HttpContext.Current.Response; }
        }

        public static HttpApplicationState CurHttpApplication
        {
            get { return HttpContext.Current.Application; }
        }

        public static System.Web.Caching.Cache CurCache
        {
            get { return HttpContext.Current.Cache; }
        }

        public static HttpServerUtility CurServer
        {
            get { return HttpContext.Current.Server; }
        }
        #endregion

        #region 取出站点本地根路径

        /// <summary>
        /// 取出站点本地路径
        /// </summary>
        /// <returns></returns>
        public static string GetPath()
        {
            return CurServer.MapPath("~");
        }


        #endregion

        #region 根据KEY，取出对应路径
        /// <summary>
        /// 取出站点本地路径
        /// </summary>
        /// <param name="key">配置文件中包含对应文件路径的Key名</param>
        /// <returns>String Path</returns>
        public static string GetPath(string key)
        {
            string path = GetPath();
            if (GetAppSettings(key) != "")
            {
                string temp = GetAppSettings(key);
                if (temp.IndexOf("/") != -1)
                {
                    temp = temp.Replace("/", "\\");
                }
                path = System.IO.Path.Combine(path, temp);
            }
            return path;
        }
        #endregion

        #region 根据KEY，取出对应的appSettings值
        /// <summary>
        /// 取出Key 对应的appSettings值
        /// </summary>
        public static string GetAppSettings(string key)
        {
            string reValue = "";
            if (ConfigurationManager.AppSettings[key] != null)
            {
                reValue = ConfigurationManager.AppSettings[key].ToString();
            }
            return reValue;
        }
        /// <summary>
        /// 取出Key 对应的appSettings值
        /// </summary>
        public static string GetServerPath(string key)
        {

            return CurContext.Server.MapPath(key);
        }
        #endregion

        #region 取出根目录的URL地址
        /// <summary>
        /// 得到根目录的URL地址
        /// </summary>
        public static string GetBaseURL()
        {
            //string strPort = CurHttpRequest.Url.Port == 80 ?
            //    "" : ":" + CurHttpRequest.Url.Port.ToString();

            //if (CurHttpRequest.ApplicationPath == "/")
            //{
            //    return @"http://" + CurHttpRequest.Url.Host + strPort;
            //}
            //else
            //{
            //    return @"http://" + CurHttpRequest.Url.Host + strPort + CurHttpRequest.ApplicationPath;
            //}
            string strPort = CurHttpRequest.Url.Port == 80 ?
                "" : ":" + CurHttpRequest.Url.Port.ToString();
            var scheme = CurHttpRequest.Url.Scheme;
            if (CurHttpRequest.ApplicationPath == "/")
            {
                return scheme + "://" + CurHttpRequest.Url.Host + strPort;
            }
            else
            {
                return scheme + "://" + CurHttpRequest.Url.Host + strPort + CurHttpRequest.ApplicationPath;
            }
        }
        /// <summary>
        /// 获取正确的网站根地址
        /// </summary>
        /// <returns></returns>
        public static string GetRightBaseURL()
        {
            string strPort = CurHttpRequest.Url.Port == 80 ?
                "" : ":" + CurHttpRequest.Url.Port.ToString();
            var scheme = CurHttpRequest.Url.Scheme;
            if (CurHttpRequest.ApplicationPath == "/")
            {
                return scheme+"://" + CurHttpRequest.Url.Host + strPort;
            }
            else
            {
                return scheme + "://" + CurHttpRequest.Url.Host + strPort + CurHttpRequest.ApplicationPath;
            }
        }
        /// <summary>
        /// 根据相对路径获取完全限定名的访问地址
        /// </summary>
        /// <param name="relativeUrl"></param>
        /// <returns></returns>
        public static string GetFullURL(string relativeUrl)
        {
            var baseUrl = GetBaseURL();
            var szReturn = baseUrl + relativeUrl;
            return szReturn;
        }
        public static string GetHttpBaseURL()
        {
            string strPort = CurHttpRequest.Url.Port == 80 ?
                  "" : ":" + CurHttpRequest.Url.Port.ToString();
            if (CurHttpRequest.ApplicationPath == "/")
            {
                return @"http://" + CurHttpRequest.Url.Host + strPort;
            }
            else
            {
                return @"http://" + CurHttpRequest.Url.Host + strPort + CurHttpRequest.ApplicationPath;
            }
        }
        #endregion

        #region 根据KEY,取出对应的URL地址
        public static string GetBaseURL(string key)
        {
            string baseUrl = "";
            if (ConfigurationManager.AppSettings[key] != null)
            {
                baseUrl = ConfigurationManager.AppSettings[key].ToString();
                if (baseUrl.IndexOf("\\") != -1)
                {
                    baseUrl = baseUrl.Replace("\\", "/");
                }
                baseUrl = StringUtility.CutLastFlag(baseUrl, "/");
                baseUrl = GetBaseURL() + "/" + baseUrl;
            }
            else
            {
                baseUrl = GetBaseURL();
            }
            return baseUrl;
        }
        #endregion

        #region 取出当前的URL地址
        /// <summary>
        /// 取出当前的URL地址
        /// </summary>
        public static string GetCurURL()
        {
            if (CurHttpRequest.ApplicationPath == "/")
            {
                return GetBaseURL() + CurHttpRequest.Path;
            }
            return GetBaseURL() + CurHttpRequest.Path.Replace(CurHttpRequest.ApplicationPath, "");
        }
        #endregion

        #region 取出当前包含参数的URL地址
        /// <summary>
        /// 取出当前包含参数的URL地址
        /// </summary>
        /// <returns></returns>
        public static string GetRawURL()
        {
            if (CurHttpRequest.ApplicationPath == "/")
            {
                return GetBaseURL() + CurHttpRequest.RawUrl;
            }
            return GetBaseURL() + CurHttpRequest.RawUrl.Replace(CurHttpRequest.ApplicationPath, "");
        }
        #endregion

        #region 取出链接到该URL的地址
        /// <summary>
        /// 取出链接到该URL的地址
        /// </summary>
        /// <returns></returns>
        public static string GetPreURL()
        {
            try
            {
                return CurHttpRequest.UrlReferrer.ToString();
            }
            catch
            {
                return GetCurURL();
            }
        }
        #endregion

        #region 取出客户端的IP地址
        //取出客户端的IP地址
        public static string GetClientIP()
        {
            return CurHttpRequest.UserHostAddress;
        }
        #endregion




    }
}
